from PySide2.QtCore import *  # type: ignore
from PySide2.QtGui import *  # type: ignore
from PySide2.QtWidgets import *  # type: ignore
from PySide2.QtWidgets import (
    QApplication,
    QMainWindow,
    QPushButton,
    QLabel,
    QVBoxLayout,
    QWidget,
)
import sys
import json
import typing
import c_code
import config
import pars
import os


class ListWidget(QListWidget):
    def __init__(self, parent: typing.Optional[QWidget] = ...) -> None:
        super().__init__(parent)
        self.setViewMode(QListView.IconMode)
        self.setSpacing(10)
        self.setIconSize(QSize(50, 50))
        # self.listWidget_4.setMovement(False)
        self.setResizeMode(QListView.Adjust)
        self.call = {}

    def clicked(self, item):
        config.obj_signal.text.emit(self.call[item.text()]["link"])

    def doubleClicked(self, item):
        str_1 = self.call[item.text()]["call_app"]
        # str_1 = f"python3 {config.path}/applications/{str_1}"
        self.execute(str_1)

    def set_data(self, config_dict_data):
        self.call = config_dict_data["call"]

    def execute(self, link):
        # link = link.encode('utf-8')
        print(link, 222)
        os.system(f"{link} > /dev/null 2>&1 &")
        # c_code.obj_c_tools.execute(link)


class Ui_MainWindow(object):
    def setting(self) -> None:
        self.lang_json = config.read_json(f"{config.path}/locale/lang.json")

    def setupUi(self, MainWindow):
        if not MainWindow.objectName():
            MainWindow.setObjectName("MainWindow")
        MainWindow.resize(853, 600)
        MainWindow.setStyleSheet(
            "\n"
            "background-color: rgb(59, 59, 59);\n"
            "color: rgb(255, 255, 255);\n"
            ""
        )
        self.centralwidget = QWidget(MainWindow)
        self.centralwidget.setObjectName("centralwidget")
        self.centralwidget.setMinimumSize(QSize(800, 600))
        self.horizontalLayout_2 = QHBoxLayout(self.centralwidget)
        self.horizontalLayout_2.setObjectName("horizontalLayout_2")
        self.horizontalLayout = QHBoxLayout()
        self.horizontalLayout.setObjectName("horizontalLayout")
        self.gridLayout = QGridLayout()
        self.gridLayout.setObjectName("gridLayout")
        self.label = QLabel(self.centralwidget)
        self.label.setObjectName("label")
        self.label.setTabletTracking(False)
        self.label.setAcceptDrops(False)
        self.label.setPixmap(
            QPixmap(f"{config.path}/images/Screenshot_20221012_081451.png")
        )

        self.gridLayout.addWidget(self.label, 0, 0, 1, 1)

        self.pushButton = QPushButton(self.centralwidget)
        self.pushButton.setObjectName("pushButton")

        self.gridLayout.addWidget(self.pushButton, 1, 0, 1, 1)

        self.horizontalLayout.addLayout(self.gridLayout)

        self.gridLayout_2 = QGridLayout()
        self.gridLayout_2.setObjectName("gridLayout_2")
        self.verticalLayout = QVBoxLayout()
        self.verticalLayout.setObjectName("verticalLayout")
        self.label_2 = QLabel(self.centralwidget)
        self.label_2.setObjectName("label_2")

        self.verticalLayout.addWidget(self.label_2)

        self.listWidget = ListWidget(self.centralwidget)
        self.listWidget.setObjectName("listWidget")

        self.verticalLayout.addWidget(self.listWidget)

        self.label_3 = QLabel(self.centralwidget)
        self.label_3.setObjectName("label_3")

        self.verticalLayout.addWidget(self.label_3)

        self.listWidget_2 = ListWidget(self.centralwidget)
        self.listWidget_2.setObjectName("listWidget_2")

        self.verticalLayout.addWidget(self.listWidget_2)

        self.label_4 = QLabel(self.centralwidget)
        self.label_4.setObjectName("label_4")

        self.verticalLayout.addWidget(self.label_4)

        self.listWidget_3 = ListWidget(self.centralwidget)
        self.listWidget_3.setObjectName("listWidget_3")

        self.verticalLayout.addWidget(self.listWidget_3)

        self.label_5 = QLabel(self.centralwidget)
        self.label_5.setObjectName("label_5")

        self.verticalLayout.addWidget(self.label_5)

        self.listWidget_4 = ListWidget(self.centralwidget)
        self.listWidget_4.setObjectName("listWidget_4")

        self.verticalLayout.addWidget(self.listWidget_4)

        self.gridLayout_2.addLayout(self.verticalLayout, 0, 0, 1, 2)

        self.horizontalSpacer = QSpacerItem(
            40, 20, QSizePolicy.Expanding, QSizePolicy.Minimum
        )

        self.gridLayout_2.addItem(self.horizontalSpacer, 1, 0, 1, 1)

        self.pushButton_close = QPushButton(self.centralwidget)
        self.pushButton_close.setObjectName("pushButton_close")

        self.gridLayout_2.addWidget(self.pushButton_close, 1, 1, 1, 1)

        self.horizontalLayout.addLayout(self.gridLayout_2)

        self.horizontalLayout_2.addLayout(self.horizontalLayout)

        MainWindow.setCentralWidget(self.centralwidget)

        self.retranslateUi(MainWindow)

        QMetaObject.connectSlotsByName(MainWindow)

    # setupUi

    def retranslateUi(self, MainWindow):
        MainWindow.setWindowTitle(
            QCoreApplication.translate("MainWindow", "MainWindow", None)
        )
        self.label.setText("")
        text_b_reference = self.lang_json["button_reference"][config.lang_os]
        text_b_close = self.lang_json["button_close"][config.lang_os]
        text_l_personal = self.lang_json["Personal"][config.lang_os]
        text_l_equipment = self.lang_json["Equipment"][config.lang_os]
        text_l_system = self.lang_json["System"][config.lang_os]
        text_l_other = self.lang_json["Other"][config.lang_os]
        self.pushButton.setText(
            QCoreApplication.translate("MainWindow", text_b_reference, None)
        )
        self.label_2.setText(
            QCoreApplication.translate("MainWindow", text_l_personal, None)
        )
        self.label_3.setText(
            QCoreApplication.translate("MainWindow", text_l_equipment, None)
        )
        self.label_4.setText(
            QCoreApplication.translate("MainWindow", text_l_system, None)
        )
        self.label_5.setText(
            QCoreApplication.translate("MainWindow", text_l_other, None)
        )
        self.pushButton_close.setText(
            QCoreApplication.translate("MainWindow", text_b_close, None)
        )

    # retranslateUi
    def set_data(self, call):
        self.listWidget.set_data(call)
        self.listWidget_2.set_data(call)
        self.listWidget_3.set_data(call)
        self.listWidget_4.set_data(call)
